package com.cs.contract.controller;

import com.alibaba.fastjson.JSONObject;
import com.cs.contract.service.CsCodeRulesService;
import com.cs.contract.service.CsFrameManagementService;
import com.cs.contract.service.CsInventoryService;
import com.cs.contract.domain.CodeRulesResult;
import com.cs.contract.domain.CsFrameManagement;
import com.cs.contract.domain.CsInventory;
import com.cs.contract.util.CodeRuleHelp;
import com.cs.contract.util.CodeRuleUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author：zrx
 * @Package：com.cs.controller
 * @Project：RuoYi-Cloud
 * @name：CsFrameManagementController
 * @Date：2024/11/28 9:31
 * @Filename：CsFrameManagementController
 */
@RestController
@RequestMapping("/management")
public class CsFrameManagementController extends BaseController {
    private final CsFrameManagementService csFrameManagementService;
    private final CsInventoryService csInventoryService;
    private final CsCodeRulesService csCodeRulesService;

    public CsFrameManagementController(CsFrameManagementService csFrameManagementService, CsInventoryService csInventoryService, CsCodeRulesService csCodeRulesService) {
        this.csFrameManagementService = csFrameManagementService;
        this.csInventoryService = csInventoryService;
        this.csCodeRulesService = csCodeRulesService;
    }

    /**
     * 删除框架协议管理
     */
    @GetMapping("/delXy")
    @Transactional
    public AjaxResult remove(Integer oid) {
        System.out.println(oid);
        int i = csInventoryService.deleteCsInventoryByOid(oid);
        if (i > 0) {
            int i1 = csFrameManagementService.deleteCsFrameManagementByOid(oid);
            if (i1 > 0) {
                return AjaxResult.success("删除成功");
            }
        }
        return AjaxResult.error("删除失败");
    }

    /**
     * 查询框架协议管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CsFrameManagement csFrameManagement) {
        startPage();
        List<CsFrameManagement> list = csFrameManagementService.listCsFrameManagement(csFrameManagement);
        return getDataTable(list);
    }

    /**
     * 协议作废
     */
    @GetMapping("/XyCancel")
    public AjaxResult XyCancel(Integer oid) {
        int i = csFrameManagementService.updateXyCancel(oid);
        if (i > 0) {
            CsFrameManagement frameManagement = new CsFrameManagement();
            frameManagement.setOid(oid);
            frameManagement.setoHstatus(5);
            int i1 = csFrameManagementService.updateCsFrameManagement(frameManagement);
            if (i1 > 0) {
                return AjaxResult.success("修改成功");
            }
            return AjaxResult.error("修改失败");
        }
        return AjaxResult.error("修改失败");
    }

    /**
     * 新增框架协议管理
     */
    @PostMapping
    public AjaxResult add(@RequestBody CsFrameManagement csFrameManagement) {
        System.out.println(csFrameManagement);
        // 创建编码
        CodeRulesResult result = CodeRuleHelp.createCode(csCodeRulesService, CodeRuleUtil.FRAMEWORK_MANAHEMENT);
        // 获取已经匹配的规则
        String oCode = result.getCode();
        // 生成协议编号
        csFrameManagement.setoCode(oCode);
        // 协议创建人
        String userName = SecurityUtils.getLoginUser().getSysUser().getUserName();
        csFrameManagement.setCreateBy(userName);
        int k = csFrameManagementService.insertCsFrameManagement(csFrameManagement);
        if (k != 0) {
            Integer oid = csFrameManagement.getOid();
            List<CsInventory> list = csFrameManagement.getCsInventoryList();
            for (CsInventory bsInventory : list) {
                bsInventory.setOid(oid);
                csInventoryService.insertCsInventory(bsInventory);
            }
            CsFrameManagement frameManagement = csFrameManagementService.getCsFrameManagementByOid(oid);
            System.out.println("kuangjia:" + frameManagement);
            JSONObject object = new JSONObject();
            object.put("frameManagement", frameManagement);
            return AjaxResult.success(object);
        }
        return AjaxResult.error("添加异常");
    }

    /**
     * 获取框架协议管理详细信息
     */
    @GetMapping(value = "/{oid}")
    public AjaxResult getInfo(@PathVariable("oid") Integer oid) {
        return success(csFrameManagementService.getCsFrameManagementByOid(oid));
    }

    /**
     * 修改框架协议管理
     */
    @PutMapping
    public AjaxResult edit(@RequestBody CsFrameManagement csFrameManagement) {
        System.out.println("打印csFrameManagement--------------------------------------");
        System.out.println(csFrameManagement);
        int k = csFrameManagementService.updateCsFrameManagement(csFrameManagement);
        if (k > 0) {
            if (csFrameManagement.getoOpinion() != null) {
                return AjaxResult.success("修改成功");
            }
            Integer oid = csFrameManagement.getOid();
            // 删除设备信息
            csInventoryService.deleteCsInventoryByOid(oid);
            // 修改设备信息
            List<CsInventory> list = csFrameManagement.getCsInventoryList();
            for (CsInventory csInventory : list) {
                csInventory.setOid(oid);
                int i = csInventoryService.insertCsInventory(csInventory);
                if (i == 0) {
                    return AjaxResult.error("修改异常");
                }
            }
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("添加异常");
    }
}
